<?php
/*
 * @Date: 2021-01-30 22:55:06
 * @名称: 
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-02-04 22:48:31
 * @FilePath: /Site/app/useradmin/controller/Comics/Operation/ComicsEdit.php
 */

namespace app\useradmin\controller\Comics\Operation;

use think\facade\Db;

class ComicsEdit
{
    /**
     * @方法名: 保存漫画信息
     * @作者: 初雪桜
     * @信息: 编辑漫画保存漫画
     * @param Json $user_info
     * @param Json $postData
     * @return {*}
     */
    public function SaveComicsEdit($user_info, $postData)
    {
        # 判断我有没有保存的权限 
        $author_and_chinese_team =  Db::table('author_and_chinese_team')
            ->where([
                "user_id" => $user_info["_id"],
                "book_id" => $postData['bookId'],
                "state" => 0,
            ])
            ->field([
                "edit_sever_comics_info"
            ])
            ->find();
        if ($author_and_chinese_team == null || $author_and_chinese_team["edit_sever_comics_info"] != "1") {
            return errorJsonReturn('Insufficient user rights'); //用户权限不足
        }
        //!验证 标签
        $tag = ''; //要插入的标签
        foreach ($postData['tags'] as $key => $value) {
            if (strpos($value["name"], '建标签:')) {
                // 插入到标签表
                $tags = Db::table('tags')
                    ->where([
                        'tag_name' => $value["value"]
                    ])
                    ->find();
                if (!$tags) {
                    Db::table('tags')
                        ->insert([
                            "tag_name" => $value["value"],
                            "_id" => randIdStructure(2),
                            "user_id" => $user_info['_id'],
                            "state" => '1',
                        ]);
                }
            }
            //* 标签 保存
            if ($key == 0) {
                $tag .= "$value[value]";
            } else {
                $tag .= ",$value[value]";
            }
        }

        DB::table("book_info") //查询漫画标签
            ->where([
                "_id" => $postData['bookId'],
            ])
            ->update([
                "tags" => $tag
            ]);
        //* 标签 保存
        //* 分类 离散
        //查询所有分类 
        $AllClassifiedCatalogue = Db::table('classified_catalogue')
            ->field([
                "_id",
                "title",
            ])
            ->select();
        foreach ($AllClassifiedCatalogue as $key => $value) {
            $AllClassifiedCatalogueJson[$value["title"]] = $value["_id"];
        }
        unset($AllClassifiedCatalogue); //回收变量

        // 查询本来的分类
        $categoriesStr = Db::table('book_info')
            ->where([
                "_id" => $postData['bookId'],
            ])
            ->field([
                "categories"
            ])
            ->find();
        //我自己的分类 离散
        $categories = explode(',', $categoriesStr["categories"]);
        $categoriesJson = [];
        foreach ($postData['categories'] as $key => $PostDataValue) {
            $categoriesJson[$PostDataValue["name"]] = $PostDataValue;
        }

        $newCategories = '';

        //将我原来标签和新传入标签对比 删除一样的
        foreach ($categories as $key => $categoriesValue) {

            if (array_key_exists($categoriesValue, $categoriesJson)) {
                unset($categoriesJson[$categoriesValue]);
                $newCategories .= ",$categoriesValue";
            } else {
                $categoriesJson[$categoriesValue] = true;
            }
        }
        //开始插入 删除标签
        foreach ($categoriesJson as $key => $categoriesJsonValue) {
            //categoriesJson 内容应该是
            //   ["百合花園"] =&gt; array(3) {//要插入的数据
            //     ["name"] =&gt; string(12) "百合花園"
            //     ["value"] =&gt; string(24) "5821859b5f6b9a4f93dbf613"
            //     ["selected"] =&gt; string(5) "false"
            //   }
            //   ["嗶咔漢化"] =&gt; bool(true)//要删除的数据
            //   ["短篇"] =&gt; bool(true)//要删除的数据
            //   其他的就被抵消了
            if ($categoriesJsonValue === true) {
                //删除数据
                Db::table("book_info_categories_info_$AllClassifiedCatalogueJson[$key]")
                    ->where(["book_id" => $postData['bookId']])
                    ->delete();
            } else {
                $newCategories .= ",$categoriesJsonValue[name]";
                Db::table("book_info_categories_info_$AllClassifiedCatalogueJson[$key]")
                    ->insert([
                        '_id' => randIdStructure(3),
                        'book_id' => $postData['bookId'],
                        'state' => 1,
                        'add_user_id' => $user_info["_id"],
                        'add_time' => date("Y-m-d H:i:s"),
                    ]);
            }
        }
        // 更新表中的记录
        $categoriesStr = Db::table('book_info')
            ->alias('book_info')
            ->join('book_list', "book_list._id = book_info._id")
            ->where([
                "book_info._id" => $postData['bookId'],
            ])
            ->update([
                "book_list.title" => $postData['title'],
                "book_info.description" => $postData['description'],
                "book_info.categories" => substr($newCategories, 1),
                "book_info.allow_comment" => ($postData['comment'] == "true") ? 1 : 0,
                "book_info.allow_download" => ($postData['download'] == "true") ? 1 : 0,
            ]);
        // dump($postData['categories']);
        //* 分类 保存
        return successJsonReturn('success');
    }
}
